<?php
Class Sitemap_model extends CI_Model
{
	function index($limit = false)
	{
		$this->load->model('Product_model');
		if($limit)
		{
			$this->db->limit($limit);
		}
		$query= $this->db->order_by('route ASC')->get('routes');
		$link=array();
		foreach ($query->result() as $route){
			$slug=$route->slug;
			$extractRoute=explode('/',$route->route);
			if(isset($extractRoute[1]) and in_array($extractRoute[1],array('product','category'))){
				if($extractRoute[1]=='product'){
					$product= $this->Product_model->get_product($extractRoute[2])->categories;
					if(isset($product[0])){
						$slug=$product[0]->slug.'/'.$slug;
						if($product[0]->parent_id){
							$category_parent= $this->db->get_where('categories', array('id' => $category->parent_id),1)->row();
							$slug=$category_parent->slug.'/'.$slug;
						}
					}
				}
				if($extractRoute[1]=='category'){
					if(isset($extractRoute[2])){
						$category= $this->db->get_where('categories', array('id' =>$extractRoute[2]),1)->row();
						if($category->parent_id){	
							$category_parent= $this->db->get_where('categories', array('id' => $category->parent_id),1)->row();
							$slug=$category_parent->slug.'/'.$slug;
						}
					}
				}
			}
			
			$link[]=$slug;
		}
		return $link;
	}
}